{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9dc75673",
   "metadata": {},
   "source": [
    "# HTTP Query - urllib3, json query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c5bc67c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Query Succeed\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "data='{\"returnCode\":\"0\",\"returnMessage\":\"Query Succeed\",\"rowCount\":\"1\",\"colCount\":\"3\",\"requestParams\":\"datacode=SURF_CHN_MUL_HOR&staids=54823&timerange=[20190412120000,20190412120000]&elements=TEM,TEM_Max,TEM_Min\",\"requestTime\":\"2019-04-13 14:17:46\",\"responseTime\":\"2019-04-13 14:17:46\",\"takeTime\":\"0.016\",\"fieldNames\":\"温度/气温 最高气温 最低气温\",\"fieldUnits\":\"摄氏度(℃) 摄氏度(℃) 摄氏度(℃)\",\"DS\":[{\"TEM\":\"14.7000\",\"TEM_Max\":\"15.1000\",\"TEM_Min\":\"14.7000\"}]}'\n",
    "\n",
    "jsonstr=json.loads(data)\n",
    "print(jsonstr[\"returnMessage\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "206bf26d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200 {\"data\":{\"id\":\"ethereum\",\"rank\":\"2\",\"symbol\":\"ETH\",\"name\":\"Ethereum\",\"supply\":\"119501995.6240000000000000\",\"maxSupply\":null,\"marketCapUsd\":\"371657691720.1711659594600498\",\"volumeUsd24Hr\":\"11067896512.0745721092520388\",\"priceUsd\":\"3110.0542696337186815\",\"changePercent24Hr\":\"-1.5232308694735880\",\"vwap24Hr\":\"3121.6250483370374313\",\"explorer\":\"https://etherscan.io/\"},\"timestamp\":1644371754444}\n"
     ]
    }
   ],
   "source": [
    "import urllib3\n",
    "coin_url = 'https://api.coincap.io/v2/assets/ethereum/'\n",
    "http = urllib3.PoolManager()\n",
    "response = http.request('GET', coin_url)\n",
    "print(response.status,response.data.decode('utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8ade8408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 'ethereum',\n",
       " 'rank': '2',\n",
       " 'symbol': 'ETH',\n",
       " 'name': 'Ethereum',\n",
       " 'supply': '119501995.6240000000000000',\n",
       " 'maxSupply': None,\n",
       " 'marketCapUsd': '371657691720.1711659594600498',\n",
       " 'volumeUsd24Hr': '11067896512.0745721092520388',\n",
       " 'priceUsd': '3110.0542696337186815',\n",
       " 'changePercent24Hr': '-1.5232308694735880',\n",
       " 'vwap24Hr': '3121.6250483370374313',\n",
       " 'explorer': 'https://etherscan.io/'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd = json.loads(response.data.decode('utf-8'))\n",
    "dd['data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6830177a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3110.0542696337186815'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dd['data']['priceUsd']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f8298960",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3110.054269633719"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(dd['data']['priceUsd'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c497858b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
